package com.szpxt.action;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.opensymphony.xwork2.ActionSupport;
import com.szpxt.model.Privilege;
import com.szpxt.model.Role;
import com.szpxt.service.PrivilegeService;
import com.szpxt.service.RoleService;

@Component @Scope("prototype")
public class RoleAction extends ActionSupport implements ServletRequestAware, ServletResponseAware, SessionAware {

	private HttpServletRequest request;
	private HttpServletResponse response;
	private Map<String, Object> sessionMap;
	
	private RoleService roleService;
	private PrivilegeService privilegeService;
	
	public void getTree() throws IOException {
		JSONArray array = new JSONArray();
		
		List<Role> listRole = roleService.getAll();
		for(Role element: listRole) {
			JSONObject object = new JSONObject();
			object.put("id", element.getId());
			object.put("text", element.getName());
			object.put("leaf", true);
			
			array.add(object);
		}
		
		response.setContentType("application/json;UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.getWriter().write(array.toString());
		response.getWriter().flush();
		response.getWriter().close();
	}
	
	public void saveOrUpdate() throws IOException {
		Map map = request.getParameterMap();
		Role role = roleService.saveOrUpdate(map);
		
		JSONObject jo = new JSONObject();
		
		JSONObject object = new JSONObject();
		object.put("id", role.getId());
		object.put("name", role.getName());
		
		jo.put(SUCCESS, true);
		jo.put("data", object);
		
		response.setContentType("application/json;UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.getWriter().write(jo.toString());
		response.getWriter().flush();
		response.getWriter().close();
	}
	
	public void get() throws IOException {
		JSONObject jo = new JSONObject();
		long id = Long.parseLong(request.getParameter("id"));
		Role role = roleService.get(id);
		
		JSONObject object = new JSONObject();
		object.put("id", role.getId());
		object.put("name", role.getName());
		JSONObject objectPrivilege = new JSONObject();
		for(Privilege privilege: role.getListPrivilege()) {
			objectPrivilege.put(privilege.getKey(), "on");
		}
		object.put("privilege", objectPrivilege);
		
		jo.put(SUCCESS, true);
		jo.put("data", object);
		
		response.setContentType("application/json;UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.getWriter().write(jo.toString());
		response.getWriter().flush();
		response.getWriter().close();
	}
	
	public void delete() throws IOException {
		long id = Long.parseLong(request.getParameter("id"));
		roleService.delete(id);
		
		JSONObject jo = new JSONObject();
		jo.put(SUCCESS, true);
		
		response.setContentType("application/json;UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.getWriter().write(jo.toString());
		response.getWriter().flush();
		response.getWriter().close();
	}
	
	public void getAll() throws IOException {
		JSONObject jo = new JSONObject();
		
		List<Role> listRole = roleService.getAll();
		JSONArray array = new JSONArray();
		for(Role role: listRole) {
			JSONObject object = new JSONObject();
			object.put("id", role.getId());
			object.put("name", role.getName());
			
			array.add(object);
		}
		
		jo.put(SUCCESS, true);
		jo.put("data", array);
		
		response.setContentType("application/json;UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.getWriter().write(jo.toString());
		response.getWriter().flush();
		response.getWriter().close();
	}
	
	public void getAllPrivilege() throws IOException {
		JSONObject jo = new JSONObject();
		
		List<Privilege> listPrivilege = privilegeService.getAll();
		JSONArray array = new JSONArray();
		for(Privilege element: listPrivilege) {
			JSONObject object = new JSONObject();
			object.put("boxLabel", element.getName());
			object.put("name", element.getKey());
			
			array.add(object);
		}
		
		jo.put(SUCCESS, true);
		jo.put("data", array);
		
		response.setContentType("application/json;UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.getWriter().write(jo.toString());
		response.getWriter().flush();
		response.getWriter().close();
	}
	
	public void logout() throws IOException {
		sessionMap.clear();
		
		JSONObject jo = new JSONObject();
		jo.put(SUCCESS, true);
		
		response.setContentType("application/json;UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.getWriter().write(jo.toString());
		response.getWriter().flush();
		response.getWriter().close();
	}

	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;		
	}
	
	@Override
	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}
	
	//获取和设置方法
	public RoleService getRoleService() {
		return roleService;
	}

	public void setRoleService(RoleService roleService) {
		this.roleService = roleService;
	}

	public PrivilegeService getPrivilegeService() {
		return privilegeService;
	}

	public void setPrivilegeService(PrivilegeService privilegeService) {
		this.privilegeService = privilegeService;
	}

	@Override
	public void setSession(Map<String, Object> sessionMap) {
		this.sessionMap = sessionMap;
	}

}
